local skynet = require "skynet"

local table_unpack = table.unpack

local data_workers = {}
local CMD = {}

local function init(worker_num)
	for i = 1,worker_num do
        local act_data_interace = skynet.newservice('act_data_interace')
	    skynet.call(act_data_interace, "lua", "start")

		table.insert(data_workers,act_data_interace)
	end
end

function CMD.start()
	print('start....')

	local worker_num = tonumber(skynet.getenv("data_interace_num")) or 4
	init(worker_num)

	skynet.dispatch('lua',function(_,_,cmd,act_id, ...)
		local i = act_id % #data_workers + 1
		local r = {skynet.call(data_workers[i],'lua',cmd,act_id,...)}
		if #r > 0 then
			skynet.retpack(table_unpack(r))
		end
	end)

	skynet.retpack(true)
end

skynet.start(function()
	skynet.dispatch("lua", function(session, source, cmd, ...)
		local f = assert(CMD[cmd],'unknown cmd ' .. cmd)
		f(...)
	end)
end)